﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Util.SQLHelpers;

namespace AllSqlHelperWin.SQLHelperForms.OracleForms
{
    public partial class OracleAddForm : Form
    {
        public OracleAddForm()
        {
            InitializeComponent();
        }

        private void MysqlAddForm_Load(object sender, EventArgs e)
        {
            cmbClass.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不可为空！");
                return;
            }

            string classStr = cmbClass.Text.Trim();
            string ageStr = txtAge.Text.Trim();
            int age = Convert.ToInt32(ageStr);

            bool gender = rdBtnB.Checked;

            //UserModel model = new UserModel()
            //{
            //    Name = name,
            //    Class = classStr,
            //    Age = string.IsNullOrEmpty(age) ? 0 : Convert.ToInt32(age),
            //    Gender = gender
            //};

            OracleParameter[] param = new OracleParameter[] {
               new OracleParameter("@Name",name),
               new OracleParameter("@Class",classStr),
               new OracleParameter("@Age",age),
               new OracleParameter("@Gender",gender),
               new OracleParameter("@CreateTime",DateTime.Now)
            };

            int addResult = OracleSqlHelper.ExecuteNonQuery("INSERT INTO User(Name, Class, Age, Gender, CreateTime) VALUES (@Name, @Class, @Age, @Gender, @CreateTime)", param);
            if (addResult > 0)
            {
                MessageBox.Show("添加成功！");
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("名称不可为空！");
                return;
            }

            string classStr = cmbClass.Text.Trim();
            string ageStr = txtAge.Text.Trim();
            int age = Convert.ToInt32(ageStr);

            bool gender = rdBtnB.Checked;

            //UserModel model = new UserModel()
            //{
            //    Name = name,
            //    Class = classStr,
            //    Age = string.IsNullOrEmpty(age) ? 0 : Convert.ToInt32(age),
            //    Gender = gender
            //};

            OracleParameter[] param = new OracleParameter[] {
               new OracleParameter("@Name",name),
               new OracleParameter("@Class",classStr),
               new OracleParameter("@Age",age),
               new OracleParameter("@Gender",gender),
               new OracleParameter("@CreateTime",DateTime.Now)
            };

            object addResult = OracleSqlHelper.ExecuteScalar("INSERT INTO User(Name, Class, Age, Gender, CreateTime) VALUES (@Name, @Class, @Age, @Gender, @CreateTime);select @@identity", param);
            if (addResult != null)
            {
                MessageBox.Show($"添加成功！Id为{Convert.ToInt32(addResult)}");
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
        }

        /// <summary>
        /// 只能输入数字
        /// </summary>
        private void txt1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是退格和数字，则屏蔽输入
            if (!(e.KeyChar == 8 || (e.KeyChar >= 48 && e.KeyChar <= 57)))  // 小数点为46
            {
                e.Handled = true;
            }
        }
    }
}
